@extends('admin.common.base-master')

@section('html-head')
    <link href="{{url('css/admin/pagenum.css')}}" rel="stylesheet" />
    <!-- 配置文件 -->
    <script type="text/javascript" src="{{url('vendor/ueditor/ueditor.config.js')}}"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="{{url('vendor/ueditor/ueditor.all.js')}}"></script>
@endsection

@section('html-body')
        <!-- content start -->
    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <a href="{{route('Admin.Index.doctorList')}}">医生管理</a>
            <em>></em>
            <a >@if($doctor && $doctor->id)修改医生信息 @else 添加医生信息 @endif</a>
        </div>
        <!-- cur-position end -->

        <div class="list-wrap">
            <h2 class="head">
                <span class="span1">医生资料</span>
            </h2>

            <!-- list-text-box start -->
            <form id="addAdvertiserInfo">
                <div class="list-text-box">
                    <div class="fill-list">
                        <label class="fill-tit">医生名称：</label>
                        <div class="fill-cont">
                            <input class="fill-ipt" type="text" placeholder="请输入医生名称" name="name" id="name" value="@if($doctor){{$doctor->doc_name}}@endif" /><span id="namere" class="required">*</span>
                        </div>
                    </div>
                    <div class="fill-list">
                        <label class="fill-tit">性别：</label>
                        <div class="fill-cont" style="margin-top: 5px">
                            <input type="radio" name="sex" value="1" @if($doctor && (int)$doctor->sexual == 1 || !$doctor)checked @endif>男
                            <input type="radio" name="sex" value="0" @if($doctor && (int)$doctor->sexual == 0)checked @endif style="margin-left: 10px">女
                        </div>
                    </div>
                    <div class="fill-list">
                        <label class="fill-tit">医生头像：</label>
                        <div class="fill-cont">
                            <a class="fill-file-box" href="javascript:;" id="inputImage"></a>
                            <input type="hidden" id="licence_id" name="photo" value="@if($doctor){{$doctor->doc_img}}@endif"/>
                            <span class="lx" id="licence_msg">@if($doctor && (int)$doctor->doc_img !== 0)<img width="40px" height="40px" src="{{route('Api.Image.Show',['id'=>$doctor->doc_img])}}"> @else上传JPG ,PNG ,BMP ,GIF等格式 @endif</span>
                        </div>
                    </div>
                    <div class="fill-list">
                        <label class="fill-tit">所在医院：</label>
                        <div class="fill-cont">
                            <select id="hospital_list" name="hospital_list">
                                @foreach($hospitals as $hospital)
                                    <option value="{{$hospital->id}}" @if($doctor && $hospital->id == $doctor->hospital_id)selected @endif>{{$hospital->name}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="fill-list">
                        <label class="fill-tit">主治科目：</label>
                        <div class="fill-cont">
                            <select id="dep_list" name="dep_list">
                                <option value="0">请选择科室</option>
                                @foreach($deps as $dep)
                                    <option value="{{$dep->id}}" @if($doctor && $dep->id == $doctor->dep_id)selected @endif>{{$dep->dep_name}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="fill-list">
                        <label class="fill-tit">岗位级别：</label>
                        <div class="fill-cont" style="margin-top: 5px">
                            <select class="sh30" name="station" id="doc_type">
                                <option value="主任医生" @if($doctor && $doctor->station == '主任医生')selected @endif>主任医生</option>
                                <option value="副主任医生" @if($doctor && $doctor->station == '副主任医生')selected @endif>副主任医生</option>
                                <option value="主治医生" @if($doctor && $doctor->station == '主治医生')selected @endif>主治医生</option>
                            </select>
                        </div>
                    </div>
                    <div class="fill-list">
                        <label class="fill-tit">预约费用：</label>
                        <div class="fill-cont">
                            <input class="fill-ipt" type="text" placeholder="请输入医生预约费用" name="fee" id="fee" value="@if($doctor){{$doctor->fee}}@else 7.00 @endif" /><span id="namere" class="required">*</span>
                        </div>
                    </div>
                    <div class="fill-list">
                        <label class="fill-tit">医生手机号：</label>
                        <div class="fill-cont">
                            <input class="fill-ipt" type="text" placeholder="请输入医生预约费用" name="phone" id="phone" value="@if($doctor){{$doctor->phone}}@else 7.00 @endif" /><span id="namere" class="required">*</span>
                        </div>
                    </div>
                    <div class="fill-list">
                        <label class="fill-tit">医生简介：</label>
                        <div class="fill-cont">
                            <textarea class="fill-texta" placeholder="请输入简介" name="introduction">@if($doctor){{$doctor->introduction}}@endif</textarea>
                        </div>
                    </div>
                    {{--<div class="gg-list" style="padding-left: 149px;">--}}
                        {{--<label class="tit">详细介绍</label>--}}
                        {{--<div class="cont">--}}
                            {{--<textarea class="textarea" placeholder="请输入活动备注" name="adtips" id="adtips" style="border: none"></textarea>--}}
                        {{--</div>--}}
                    {{--</div>--}}

                    {{--<div class="fill-list">--}}
                        {{--<label class="fill-tit">显示版块：</label>--}}
                        {{--<div class="fill-cont">--}}
                            {{--<input type="radio" name="state" value="0">专家介绍--}}
                            {{--<input type="radio" name="state" value="1" checked> 出诊医生--}}
                        {{--</div>--}}
                    {{--</div>--}}
                    <div class="fill-list">
                        <label class="fill-tit">状态：</label>
                        <div class="fill-cont" style="margin-top: 5px">
                            <input type="radio" name="iswork" value="1" @if($doctor && (int)$doctor->is_work == 1 || !$doctor)checked @endif> 正常出诊
                            <input type="radio" name="iswork" value="0" @if($doctor && (int)$doctor->is_work == 0)checked @endif>不出诊
                        </div>
                    </div>
                    <input type="hidden" id="id" name="id" value="@if($doctor){{$doctor->id}}@else 0 @endif" />
                    <div class="fill-list">
                        <label class="fill-tit">&nbsp;</label>
                        <div class="fill-cont">
                            <input class="fill-submit" type="button" value="提交" />
                        </div>
                    </div>
                </div>
            </form>
            <!-- list-text-box end -->
        </div>
    </div>
    <!-- content end -->
    <script>
        jQuery(function($) {
//            <!-- 实例化编辑器 -->
//            var ue = UE.getEditor('adtips',{
//                autoHeightEnabled: true,
//                initialFrameHeight:400,
//                toolbars: [
//                    ['fullscreen', 'source', 'undo','redo','|','fontsize','horizontal','|','simpleupload','removeformat'],
//                    ['bold', 'italic', 'underline', 'forecolor', 'backcolor','|','indent','|','justifyleft', 'justifyright', 'justifycenter', 'justifyjustify', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight','|','insertunorderedlist', 'insertorderedlist','|', 'imagenone','imageleft','imageright','imagecenter','|','link']
//                ]
//            });

            $('#doc_type').change(function() {
                var selectValue = $(this).children('option:selected').val();
                if(selectValue == "主任医生" || selectValue == "副主任医生"){
                    $('#fee').val(7);
                }else if(selectValue == "主治医生"){
                    $('#fee').val(4);
                }
            });

                jQuery.ajaxSetup({
                case:false,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            var doc = document,
                    $doc = $(doc);

            $doc.on('click','.fill-submit',function(){
                var name    = $('#name').val();
//                var subject = $('#subject').val();
                var hospital_id = $('#hospital_list option:selected').val();
                var dep_id = $('#dep_list option:selected').val();
                var phone = $('#phone').val();

                if(name == '' || name.length < 1){
                    $('#namere').html('*必填')
                }else if(parseInt(dep_id) == 0 || parseInt(hospital_id) == 0){
                    alert("请选择医生所在医院和科室")
                }else if(phone == '' || phone.length < 1){
                    alert("请选择医生所在医院和科室")
                }else {
                    $.ajax({
                        type:"post",
                        url:'{{route('Api.User.addDoctor')}}',
                        dataType:"json",
                        data:$('#addAdvertiserInfo').serialize(),
                        success:function(obj){
                            if(obj.level == "success"){
                                tusi(obj.message);
                                window.location.href="{{route('Admin.Index.doctorList')}}";
                            }
                        },
                        error:function(obj){
                            var datajson = obj.responseJSON;
                            tusi(datajson.message);
                        }
                    });
                }
            });

            <!-- 医院联动查询 -->
            $('#hospital_list').change(function(){
                var id = $('#hospital_list option:selected').val();
                if(parseInt(id) != 0){
                    $.ajax({
                        url:"{{route('Api.Doctor.getDepartmentByHospitalId')}}?id="+id,
                        type:"get",
                        dataType:"json",
                        success:function (obj) {
                            if(obj.level == 'success'){
                                appendDepartmen(obj.data);
                            }else {
                                noty({'type':'error','text':obj.message,timeout:600});
                            }
                        },
                        error:function (obj) {
                            console.log(obj);
                        }
                    });
                }
            });

            //写入部门
            function appendDepartmen(data) {
                $('#dep_list').empty();
                if(data.length > 0){
                    for(var i = 0;i < data.length;i++){
                        var htmlElent = '<option value="'+data[i].id+'">'+data[i].dep_name+'</option>';
                        $('#dep_list').append(htmlElent);
                    }
                }else {
                    $('#dep_list').append('<option value="0" selected>请选择科室</option>');
                }
            }

            //上传营业执照图片
            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4',
                browse_button: 'inputImage',
                url: '{{route('Api.Upload.Image')}}',

                chunk_size: '20mb',
                filters: {
                    max_file_size: '10mb',
                    mime_types: [
                        {title: "Image files", extensions: "jpg,gif,png"}
                    ],
                    prevent_duplicates: false
                },
                multi_selection: true,
                unique_names: true,

                flash_swf_url: '{{ url('vendor/plupload-2.1.2/Moxie.swf') }}',
                silverlight_xap_url: '{{ url('vendor/plupload-2.1.2/Moxie.xap') }}',

                file_data_name: 'photo',
                headers: {
                    Accept: 'application/json'
                },

                multipart_params: {
                    _token: '{{ csrf_token() }}'
                },

                init: {
                    PostInit: function () {
                    },

                    FilesAdded: function (up, files) {
                        up.start();
                    },

                    BeforeUpload: function (up, file) {
                        up.settings.multipart_params.file_name = file.name;
                    },

                    UploadProgress: function (up, file) {

                    },

                    Error: function (up, err) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(err.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }

                        noty({
                            type: 'error',
                            text: err.message + ' ' + responseMessage,
                            timeout: 800
                        });
                    },

                    FileUploaded: function (up, file, info) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(info.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }
                        $('#licence_id').val(responseObj.data.id);
                        $('#licence_msg').html('<span style="color: green">上传成功,'+responseObj.data.img_name+'</span>');
                    }
                }
            });
            uploader.init();
        });
    </script>
@endsection